package org.vectrics.recruiter.struts.admin.job;

import javax.servlet.jsp.JspException;

import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.recruiter.job.JobFunctionType;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * @author pgoette
 * Clase encargada de guardar el Function de un Job
 */
public class SaveJobFunctionAction extends VectricsStrutsAction {


	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		
		String forward = "success";
		JobFunctionForm form = (JobFunctionForm) actionData.getForm();
		JobFunction function = null;		
		JobFunctionType jobFunctionType = null;
		if (actionData.wasButtonPressed("cancel")) {
			return forward;
		}
		
		function = RecruitServiceLocator.getJobProvider().findFunction(form.getCode());
		if (function == null) {
			function = new JobFunction();
			jobFunctionType = new JobFunctionType();
		} 
		
		function.setCode(form.getCode());
		function.setDescription(form.getDescription());
		function.setWeight(form.getWeight());
		function.setProfileLink(form.getProfileLink());
		try {
			jobFunctionType = RecruitServiceLocator.getJobProvider().findAllJobFunctionsTypeById(form.getJobFunctionTypeId());
			function.setJobFunctionType(jobFunctionType);
		} catch (Exception e) {
			
		}
		
		RecruitServiceLocator.getJobProvider().saveJobFunction(function);
		
		return forward;
	}

}
